/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testonlineconnectdb;

import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author AngelKid
 */
public class SaxProcessGetQuestion extends DefaultHandler{
    boolean flagFound,flagUpdate;
    String[] qIndex;
    int codeFound = 0;
    Question question;
    ArrayList result;
    int choice;
    boolean bCauHoi, bMaCauHoi,bDoKho,bDapAn,flagChoice,bNoiDung;
    public SaxProcessGetQuestion(String[] index)
    {
        result = new ArrayList();
        flagUpdate = flagFound = bCauHoi = bMaCauHoi = bDoKho = bDapAn = flagChoice = bNoiDung = false;
        qIndex = index;
//        for(int i =0;i< index.length;i++)
//        {
//            qIndex[i] = index[i];
//        }
    }
    public ArrayList GetQuestion()
    {
        if(flagFound)
            return result;
        else
            return null;
    }
    @Override
    public void startElement(String uri,String localName,String qName, Attributes attributes){
        if(qName.equals("CauHoi"))
        {
            question = new Question();
            choice = 0;
            bCauHoi = true;
        }
        if(qName.equals("NoiDung"))
        {
            bNoiDung = true;
        }
        if(qName.equals("LuaChon"))
        {
            choice++;
            flagChoice = true;
        }
        if(qName.equals("DapAn"))
        {
            bDapAn = true;
        }
        if(qName.equals("DoKho"))
        {
            bDoKho = true;
        }
        if(qName.equals("MaCauHoi"))
        {
            bMaCauHoi = true;
        }
        
    }
    @Override
    public void characters(char[] ch, int start, int length)
    {
        String str = new String(ch,start,length);
        if(bNoiDung)
        {
            question.ask = str;
        }
        if(bDapAn)
        {
            question.result = str;
        }
        if(bDoKho)
        {
            question.testType = str;
        }
        if(bMaCauHoi)
        {
            question.code = str;
        }
        if(flagChoice)
        {
            switch(choice)
            {
                case 1: question.choice1 = str; break;
                case 2: question.choice2 = str; break;
                case 3: question.choice3 = str; break;
                case 4: question.choice4 = str; break;    
                default:
            }
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName){
        if(qName.equals("CauHoi"))
        {
            bCauHoi = false;
            if(flagUpdate)
            {
                Question q = new Question(question);                
                result.add(q);
                flagUpdate = false;
            }
        }
        if(qName.equals("NoiDung"))
        {
            bNoiDung = false;
        }
        if(qName.equals("LuaChon"))
        {
            flagChoice = false;
        }
        if(qName.equals("DapAn"))
        {
            bDapAn = false;
        }
        if(qName.equals("DoKho"))
        {
            bDoKho = false;
        }
        if(qName.equals("MaCauHoi"))
        {
            bMaCauHoi = false;
            for(int i=0;i<qIndex.length;i++){
                if(question.code.equals(qIndex[i]) == true)
                {
                    flagFound = flagUpdate = true;
                }
            }
        }
    }
    
}
